package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
@Slf4j
public class DtsAdminAdminController {

    @Autowired
    DtsAdminService adminService;
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin) {
        Integer id = admin.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        adminService.deleteById(id);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:admin:update") //控制权限
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "编辑") //对权限的修饰
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        DtsAdmin dtsAdmin = adminService.update(admin);
        log.info("改变管理员账号成功");
        log.debug("管理员改变了什么样的账号："+dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);
    }
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin){


        DtsAdmin dtsAdmin = adminService.add(admin);
        return ResponseUtil.ok(dtsAdmin);
    }
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page",required = false, defaultValue = "1") Integer page ,
            @RequestParam(name = "limit",required = false, defaultValue = "10")Integer limit,
            String sort, String order,String username){

        IPage<DtsAdmin> ipage = adminService.queryPage(page, limit, sort, order, username);

        Map<String,Object> data = new HashMap<>();

        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);
     }
}
